package com.example.wechatapplication;

import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.ImageButton;
import android.widget.LinearLayout;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private Fragment mTab01 = new weixinFragment();
    private Fragment mTab02 = new frdFragment();
    private Fragment mTab03 = new contactFragment();
    private Fragment mTab04 = new settingFragment();

    private FragmentManager fm;

    private LinearLayout mTabWeixin;
    private LinearLayout mTabFrd;
    private LinearLayout mTabAddress;
    private LinearLayout mTabSettings;

    private ImageButton mImgWeixin;
    private ImageButton mImgFrd;
    private ImageButton mImgAddress;
    private ImageButton mImgSettings;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        getSupportActionBar().hide();                   //隐藏标题
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);

        initView();
        initFragment();
        initEven();
        selectfragment(0);
    }
    private void initFragment(){
        fm = getSupportFragmentManager();
        FragmentTransaction transaction = fm.beginTransaction();
        transaction.add(R.id.id_content,mTab01);
        transaction.add(R.id.id_content,mTab02);
        transaction.add(R.id.id_content,mTab03);
        transaction.add(R.id.id_content,mTab04);
        transaction.commit();
    }

    private void initView(){
        mTabWeixin = (LinearLayout) findViewById(R.id.id_tab_weixin);
        mTabFrd = (LinearLayout) findViewById(R.id.id_tab_frd);
        mTabAddress = (LinearLayout) findViewById(R.id.id_tab_contact);
        mTabSettings = (LinearLayout) findViewById(R.id.id_tab_setting);

        mImgWeixin = (ImageButton) findViewById(R.id.id_tab_weixin_img);
        mImgFrd = (ImageButton) findViewById(R.id.id_tab_frd_img);
        mImgAddress = (ImageButton) findViewById(R.id.id_tab_contact_img);
        mImgSettings = (ImageButton) findViewById(R.id.id_tab_setting_img);
    }

    private void hidefragment(FragmentTransaction transaction){   //传入一个transaction
        transaction.hide(mTab01);       //hide参数页面
        transaction.hide(mTab02);
        transaction.hide(mTab03);
        transaction.hide(mTab04);
    }

    private void selectfragment(int i){
        FragmentTransaction transaction = fm.beginTransaction();
        hidefragment(transaction);
        resetting();
        switch (i){
            case 0:
                transaction.show(mTab01);
                mImgWeixin.setImageResource(R.drawable.text_press);
                break;
            case 1:
                transaction.show(mTab02);
                mImgFrd.setImageResource(R.drawable.people_press);
                break;
            case 2:
                transaction.show(mTab03);
                mImgAddress.setImageResource(R.drawable.context_press);
                break;
            case 3:
                transaction.show(mTab04);
                mImgSettings.setImageResource(R.drawable.s2);
                break;
            default:break;
        }
        transaction.commit();

    }           //监听函数根据i进行图标显示

    @Override
    public void onClick(View v) {        //做监听的响应函数

        switch (v.getId()){         //获得视图的ID
            case R.id.id_tab_weixin_img:
                selectfragment(0);
                break;
            case R.id.id_tab_frd_img:
                selectfragment(1);
                break;
            case R.id.id_tab_contact_img:
                selectfragment(2);
                break;
            case R.id.id_tab_setting_img:
                selectfragment(3);
                break;
            default:
                break;
        }
    }

    private void resetting(){
        mImgWeixin.setImageResource(R.drawable.text);
        mImgFrd.setImageResource(R.drawable.people);
        mImgAddress.setImageResource(R.drawable.context);
        mImgSettings.setImageResource(R.drawable.s1);
    }   //图标更换
    private void initEven(){
        mImgWeixin.setOnClickListener(this);
        mImgFrd.setOnClickListener(this);
        mImgAddress.setOnClickListener(this);
        mImgSettings.setOnClickListener(this);
    }       //监听过程优化
}